package com.stable.web.controller;

import com.stable.service.FiveYearFinanceService;
import com.stable.utils.DateUtil;
import com.stable.utils.FileUtil;
import com.stable.vo.bus.FiveYearFinanceReturnInfo;
import com.stable.vo.http.JsonResult;
import com.stable.vo.spi.req.EsQueryPageReq;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 财务数据分析
 */
@RequestMapping("/fiveYearFinance")
@RestController
@Log4j2
public class FiveYearFinanceController {

    @Autowired
    private FiveYearFinanceService fiveYearFinanceService;


    /**
     * 最近5年的财务数据分析
     *
     * @param last5YearRoeAllPercent     ROE过去5年都高于(%)
     * @param last5YearRoeAveragePercent ROE过去5年年化高于(%)
     * @param last1YearYsPercent         过去1年营收增速(%)
     * @param last3YearYsPercent         过去3年营收增速(%)
     * @param last1YearLrPercent         过去1年利润增速(%)
     * @param last3YearLrPercent         过去3年利润增速(%)
     * @return
     */
    @RequestMapping(value = "/financeQuery", method = RequestMethod.GET)
    public ResponseEntity<JsonResult> stocklist(@RequestParam(name = "last5YearRoeAllPercent", defaultValue = "10", required = false) double last5YearRoeAllPercent,
                                                @RequestParam(name = "last5YearRoeAveragePercent", defaultValue = "15", required = false) double last5YearRoeAveragePercent,
                                                @RequestParam(name = "last1YearYsPercent", defaultValue = "30", required = false) double last1YearYsPercent,
                                                @RequestParam(name = "last3YearYsPercent", defaultValue = "100", required = false) double last3YearYsPercent,
                                                @RequestParam(name = "last1YearLrPercent", defaultValue = "30", required = false) double last1YearLrPercent,
                                                @RequestParam(name = "last3YearLrPercent", defaultValue = "100", required = false) double last3YearLrPercent,
                                                String code,
                                                String conceptName,  //相关概念
                                                String gsld,  //公司亮点
                                                Integer ysLrPassCount,//营收1、3年；利润1、3年增速四个条件满足的个数
                                                Boolean before10RoePlus, //10年前的ROE为正值
                                                EsQueryPageReq queryPage) {
        queryPage.setPageSize(1000);
        String queryDate = DateUtil.getCurYYYY() + "";
        JsonResult r = new JsonResult();
        try {
            r.setResult(fiveYearFinanceService.queryFinance(queryDate, code, last5YearRoeAllPercent, last5YearRoeAveragePercent, last1YearYsPercent, last3YearYsPercent, last1YearLrPercent, last3YearLrPercent, queryPage, conceptName, gsld, ysLrPassCount, before10RoePlus));
            r.setStatus(JsonResult.OK);
        } catch (Exception e) {
            r.setResult(e.getClass().getName() + ":" + e.getMessage());
            r.setStatus(JsonResult.ERROR);
            e.printStackTrace();
        }
        return ResponseEntity.ok(r);
    }


    /**
     * 下载文件
     *
     * @return
     */
    @RequestMapping({"/download"})
    public void download(@RequestParam(name = "last5YearRoeAllPercent", defaultValue = "10", required = false) double last5YearRoeAllPercent,
                         @RequestParam(name = "last5YearRoeAveragePercent", defaultValue = "15", required = false) double last5YearRoeAveragePercent,
                         @RequestParam(name = "last1YearYsPercent", defaultValue = "30", required = false) double last1YearYsPercent,
                         @RequestParam(name = "last3YearYsPercent", defaultValue = "100", required = false) double last3YearYsPercent,
                         @RequestParam(name = "last1YearLrPercent", defaultValue = "30", required = false) double last1YearLrPercent,
                         @RequestParam(name = "last3YearLrPercent", defaultValue = "100", required = false) double last3YearLrPercent,
                         String code,
                         String conceptName,  //相关概念
                         String gsld,  //公司亮点
                         Integer ysLrPassCount,//营收1、3年；利润1、3年增速四个条件满足的个数
                         Boolean before10RoePlus, //10年前的ROE为正值
                         EsQueryPageReq queryPage) {
        queryPage.setPageSize(1000);
        String queryDate = DateUtil.getCurYYYY() + "";
        JsonResult r = new JsonResult();
        List<FiveYearFinanceReturnInfo> newFinanceReturnInfos = fiveYearFinanceService.queryFinance(queryDate, code, last5YearRoeAllPercent, last5YearRoeAveragePercent, last1YearYsPercent, last3YearYsPercent, last1YearLrPercent, last3YearLrPercent, queryPage, conceptName, gsld, ysLrPassCount, before10RoePlus);

        String filename = "财务5年分析列表.txt";
        FileUtil.downloadFile(newFinanceReturnInfos.stream().map(FiveYearFinanceReturnInfo::getCode).collect(Collectors.toList()), filename);
    }

}
